/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.maven;

import okhttp3.OkHttpClient;

import java.net.URI;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

/**
 * @author hedyn
 */
public class MavenDownloaderBuilder {

    private Path downloadPath = null;
    private OkHttpClient httpClient = null;
    private List<Dependency> dependencies = null;
    private List<URI> repositories = null;
    private DownloadCallback downloadCallback = null;

    /**
     * Set download path
     *
     * @param path Path (non-null)
     * @return this (for chaining)
     */
    public MavenDownloaderBuilder withDownloadPath(Path path) {
        this.downloadPath = path;
        return this;
    }

    /**
     * Set {@link OkHttpClient} instance what'll be used in this {@link MavenDownloader} instance
     *
     * @param client {@link OkHttpClient} instance
     * @return this (for chaining)
     */
    public MavenDownloaderBuilder withOkHttpClient(OkHttpClient client) {
        this.httpClient = client;
        return this;
    }

    /**
     * Set depencencies list, what to download
     *
     * @param dependencies List of {@link Dependency}
     * @return this (for chaining)
     */
    public MavenDownloaderBuilder withDependencies(List<Dependency> dependencies) {
        this.dependencies = Collections.unmodifiableList(dependencies);
        return this;
    }

    /**
     * Set repositories list, where to look up dependencies
     *
     * @param repositories List of repository {@link URI}s
     * @return this (for chaining)
     */
    public MavenDownloaderBuilder withRepositories(List<URI> repositories) {
        this.repositories = Collections.unmodifiableList(repositories);
        return this;
    }

    /**
     * Set {@link DownloadCallback} to get information about downloads
     *
     * @param downloadCallback Implementation of {@link DownloadCallback}
     * @return this (for chaining)
     */
    public MavenDownloaderBuilder withDownloadCallback(DownloadCallback downloadCallback) {
        this.downloadCallback = downloadCallback;
        return this;
    }

    /**
     * Build {@link MavenDownloader} instance
     *
     * @return Instance of {@link MavenDownloader}
     */
    public MavenDownloader build() {
        if(downloadPath == null) {
            throw new IllegalStateException("Download path cannot be unset!");
        }
        if(dependencies == null) {
            dependencies = Collections.emptyList();
        }
        if(repositories == null) {
            repositories = Collections.emptyList();
        }
        if(httpClient == null) {
            httpClient = new OkHttpClient();
        }
        return new MavenDownloader(repositories, dependencies, downloadPath, httpClient, downloadCallback);
    }
}
